/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package sp.controller;

import java.util.ArrayList;
import sp.component.TextFieldListing;
import sp.form.Hal2Listing;
import sp.model.Dsartb1;
import sp.model.Dsartb4;
import sp.model.DsartoptionB4r4;

/**
 *
 * @author StephenCN
 */
public class DsartB4Controller {

    private ArrayList<Dsartb4> db4;
    private Dsartb4 dsb4;
    private final Hal2Listing halL2;
    private final Dsartb1 db1;

    public DsartB4Controller(ArrayList<Dsartb4> db4, Dsartb1 db1, Hal2Listing halL2) {
        this.db4 = db4;
        this.halL2 = halL2;
        this.db1 = db1;
    }

    public ArrayList<Dsartb4> B4hasil() {
        int j =0;
        db4 = new ArrayList<>();
        ArrayList<TextFieldListing> dummy = halL2.getListTextFieldListing();
        for (int i = 0; i < dummy.size(); i+=7) {
            dsb4 = new Dsartb4();
            dsb4.setB4r1(getString(j+1));
            dsb4.setNuart(db1.getNbs()+dsb4.getB4r1());
            dsb4.setB4r2s1(dummy.get(i).getText());
            dsb4.setB4r2s2(dummy.get(i+1).getText());
            dsb4.setB4r2s3(dummy.get(i+2).getText());
            dsb4.setB4r3(dummy.get(i+3).getText());
            dsb4.setB4r4(new DsartoptionB4r4(ubahJK(dummy.get(i+4).getText())));
            dsb4.setB4r5(dummy.get(i+5).getText());
            dsb4.setB4r6(dummy.get(i+6).getText());
            dsb4.setIsLocked("0");
            db4.add(j, dsb4);
            j++;
        }
        return db4;
    }

    public String getString(int angka) {
        String no = Integer.toString(angka);
        while (no.length() < 2) {
            no = "0" + no;
        }
        return no;
    }

    private String ubahJK(String text) {
        if(text.equals("L")) return "1";
        else if(text.equals("P")) return "2";
        else return text;
    }
}
